package com.youth.youthservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youth.commonutils.JwtUtils;
import com.youth.commonutils.R;
import com.youth.commonutils.UserDto;
import com.youth.youthservice.client.MemberClient;
import com.youth.youthservice.entity.EduComment;
import com.youth.youthservice.entity.vo.CourseVo.CommentVo;
import com.youth.youthservice.entity.vo.CourseVo.SearchObject4Comment;
import com.youth.youthservice.mapper.EduCommentMapper;
import com.youth.youthservice.service.EduCommentService;
import com.youth.youthservice.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 评论(EduComment)表服务实现类
 *
 * @author makejava
 * @since 2023-06-05 21:32:24
 */
@Service("eduCommentService")
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {


    @Autowired
    private MemberClient memberClient;

    @Autowired
    private EduCourseService courseService;

    @Override
    public R pageList(Long page, Long limit, String courseId) {
        Page<EduComment> commentPage = new Page<>(page, limit);
        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        /*根据id查询*/
        queryWrapper.eq("course_id", courseId);
        /*查询分页结果并封装到commentPage中*/
        page(commentPage, queryWrapper);
        List<EduComment> commentList = commentPage.getRecords();
        Map<String, Object> map = new HashMap<>();
        /*封装属性*/
        map.put("items", commentList);
        map.put("current", commentPage.getCurrent());
        map.put("pages", commentPage.getPages());
        map.put("size", commentPage.getSize());
        map.put("total", commentPage.getTotal());
        map.put("hasNext", commentPage.hasNext());
        map.put("hasPrevious", commentPage.hasPrevious());
        return R.ok().data(map);
    }

    @Override
    public R comment(EduComment comment, HttpServletRequest request) {
        /*解析request中的token获取用户id*/
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        /*远程调用获取用户信息*/
        UserDto loginInfo = memberClient.getLoginInfo(memberId);
        comment.setAvatar(loginInfo.getAvatar());
        comment.setNickname(loginInfo.getNickName());
        comment.setMemberId(loginInfo.getUserId());
        save(comment);
        return R.ok();
    }

    @Override
    public R pageList4BackEnd(Long page, Long limit, SearchObject4Comment searchObject) {
        /*判空*/
        LambdaQueryWrapper<EduComment> queryWrapper = new LambdaQueryWrapper<>();
        String nickname = searchObject.getNickname();
        String courseName = searchObject.getCourseName();
        Date begintime = searchObject.getBegintime();
        Date endtime = searchObject.getEndtime();
        String content = searchObject.getContent();
        /*封装查询条件*/
        queryWrapper.like(!StringUtils.isEmpty(nickname), EduComment::getNickname, nickname);
        List<String> courseIds = courseService.getCourseIdByCourseName(courseName);
        queryWrapper.in(!StringUtils.isEmpty(courseName), EduComment::getCourseId,courseIds);
        queryWrapper.between(Objects.nonNull(begintime) && Objects.nonNull(endtime), EduComment::getGmtCreate, begintime, endtime);
        queryWrapper.like(!StringUtils.isEmpty(content), EduComment::getContent, content);
        /*分页查询*/
        Page<EduComment> commentPage = new Page<>(page, limit);
        this.page(commentPage, queryWrapper);
        List<EduComment> eduComments = commentPage.getRecords();
        /*封装返回体*/
        List<CommentVo> rows = eduComments.stream().map((eduComment -> new CommentVo(
                eduComment.getId(),
                eduComment.getNickname(),
                baseMapper.getTitleById(eduComment.getCourseId()),
                eduComment.getContent(), eduComment.getGmtCreate()))).collect(Collectors.toList());
        return R.ok().data("total", commentPage.getTotal()).data("rows", rows);
    }
}

